package org.jeecg.modules.demo.crm.mapper;

import java.math.BigDecimal;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.jeecg.modules.demo.crm.dto.ChartDataDto;
import org.jeecg.modules.demo.crm.entity.CPaymentRequest;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

/**
 * @Description: 付款记录
 * @Author: jeecg-boot
 * @Date:   2025-06-20
 * @Version: V1.0
 */
public interface CPaymentRequestMapper extends BaseMapper<CPaymentRequest> {

    /**
     * 付款合计
     * @param projectId
     * @return
     */
    @Select(" select IFNULL(sum(payment_amount),0)   from c_payment_request where project_id = #{projectId}")
    BigDecimal getTotalAmount(@Param("projectId") String projectId);


    @Select(" <script> select IFNULL(sum(payment_amount),0)   from c_payment_request where project_id = #{projectId} <if test='subProjectIds != null and subProjectIds.size() > 0'>  AND sub_project_id IN <foreach collection='subProjectIds' item='id' open='(' separator=',' close=')'>#{id}</foreach> </if> </script>")
    BigDecimal getTotalAmountBySubProjectid(@Param("projectId") String projectId,@Param("subProjectIds") List<String> subProjectIds);



    @Select("select IFNULL(sum(payment_amount),0)   from c_payment_request where project_id = #{projectId} and payment_date >= DATE_FORMAT(CURDATE(), '%Y-%m-01') AND payment_date <= DATE_FORMAT(LAST_DAY(CURDATE()), '%Y-%m-%d')   ")
    BigDecimal sumMonthAmount(@Param("projectId") String projectId);


    @Select("SELECT  DATE_FORMAT(payment_date, '%Y-%m') AS 'name', sum(payment_amount) AS 'value' FROM  c_payment_request WHERE payment_date >= DATE_SUB(CURDATE(), INTERVAL 6 MONTH) and project_id= #{projectId}  GROUP BY  DATE_FORMAT(payment_date, '%Y-%m')   ORDER BY  name DESC")
    List<ChartDataDto> getChart(@Param("projectId") String projectId);

    @Select("select IFNULL(sum(payment_amount),0)   from c_payment_request where project_id = #{projectId} and sub_project_id =#{subProjectId} ")
    BigDecimal getPaymentAmount(@Param("projectId") String projectId,@Param("subProjectId") String subProjectId);
}
